package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSMutableDictionary;
import czh.cocoa.foundation.NSNumber;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIEdgeInsets;
import czh.cocoa.ui.UIEvent;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UITextAlignment;
import czh.cocoa.ui.UITouch;
import czh.cocoa.ui.UIView;

public class UIButton extends UIControl {

	private NSMutableDictionary mColors;
	
	private NSMutableDictionary mTitles;
	
	private NSMutableDictionary mImages;
	
	private NSMutableDictionary mBackgroundImages;
	
	private UILabel mTitleLabel;
	
	private UIImageView mImageView;
	
	private UIImageView mBackgroundView;
	
	private UIImageView mGlowView;
	
	private UIView mShadowView;
	
	private boolean mShowsTouchWhenHighlighted;
	
	private boolean mAdjustsImageWhenHighlighted;
	
	private boolean mAdjustsImageWhenDisabled;
	
	private UIFont mFont;
	
	private UIEdgeInsets mTitleEdgeInsets;
	
	private UIEdgeInsets mImageEdgeInsets;
	
	public UIButton() {
		 
	}
	
	public UIButton(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		mColors = new NSMutableDictionary(3);
		mTitles = new NSMutableDictionary(3);
		mImages = new NSMutableDictionary(3);
		mBackgroundImages = new NSMutableDictionary(3);
		mFont = UIFont.systemFontOfSize(18);
		mTitleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
		mImageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
	}
	
	public void setTitleColorForState(UIColor color, int state) {
		mColors.setObjectForKey(color, new NSNumber(state));
		if (mTitleLabel == null) {
			CGRect bounds = bounds().inset(mTitleEdgeInsets); 
			mTitleLabel = new UILabel(bounds);
			mTitleLabel.setFont(mFont);
			mTitleLabel.setTextAlignment(UITextAlignment.Center);
			mTitleLabel.setAutoresizingMask(UIViewAutoresizingFlexibleSize); 
			addSubview(mTitleLabel);
		}
		if((state() & state) == state) {
			mTitleLabel.setTextColor(color);
		}
	}
	
	public UIColor titleColorForState(int state) {
		return (UIColor) mColors.objectForKey(new NSNumber(state));
	}
	
	public void setTitleForState(NSString title, int state) {
		mTitles.setObjectForKey(title, new NSNumber(state)); 
		if (mTitleLabel == null) {
			CGRect bounds = bounds().inset(mTitleEdgeInsets); 
			mTitleLabel = new UILabel(bounds);
			mTitleLabel.setFont(mFont);
			mTitleLabel.setTextAlignment(UITextAlignment.Center);
			mTitleLabel.setAutoresizingMask(UIViewAutoresizingFlexibleSize); 
			addSubview(mTitleLabel);
		}
		if((state() & state) == state) {
			mTitleLabel.setText(title);
		}
	}
	
	public NSString titleForState(int state) {
		return (NSString)mTitles.objectForKey(new NSNumber(state)); 
	}
	
	public void setImageForState(UIImage image, int state) {
		mImages.setObjectForKey(image, new NSNumber(state)); 
		if (mImageView == null) {
			CGRect bounds = bounds().inset(mImageEdgeInsets);
			mImageView = new UIImageView(bounds);
			mImageView.setContentMode(UIViewContentModeCenter);
			mImageView.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
			insertSubviewBelowSubview(mImageView, mTitleLabel);
		}
		if((state() & state) == state) {
			mImageView.setImage(image);
		}
	}
	
	public UIImage imageForState(int state) {
		return (UIImage) mImages.objectForKey(new NSNumber(state));
	}
	
	public void setBackgroundImageForState(UIImage image, int state) {
		mBackgroundImages.setObjectForKey(image, new NSNumber(state)); 
		if (mBackgroundView == null) {
			CGRect bounds = bounds();
			mBackgroundView = new UIImageView(bounds);
			mBackgroundView.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
			mBackgroundView.setContentStretch(contentStretch()); 	
			insertSubviewAtIndex(mBackgroundView, 0); 
		}
		if((state() & state) == state) {
			mBackgroundView.setImage(image);
		}
	}
	
	public UIImage backgroundImageForState(int state) {
		return (UIImage) mBackgroundImages.objectForKey(new NSNumber(state));
	} 
	
	public UILabel titleLabel() {
		return mTitleLabel;
	}
	
	public UIImageView imageView() {
		return mImageView;
	}
	
	public void setShowsTouchWhenHighlighted(boolean shown) {
		mShowsTouchWhenHighlighted = shown;
		if (shown) {
			if (mGlowView == null) { 
				mGlowView = new UIImageView(bounds());
				mGlowView.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
				mGlowView.setContentMode(UIViewContentModeScaleAspectFit); 
				UIImage glow_img = UIImage.imageNamed(NSTR("cocoa/btn_glow.png"));
				mGlowView.setImage(glow_img);
				insertSubviewBelowSubview(mGlowView, mTitleLabel);
			}
			if((state() & UIControlStateHighlighted) == UIControlStateHighlighted) {
				mGlowView.setHidden(false);
			} else {
				mGlowView.setHidden(true);
			}
		} else {
			if (mGlowView != null) {
				mGlowView.removeFromSuperview();
				mGlowView = null; 
			}
		}
	}
	
	public boolean isShowsTouchWhenHighlighted() {
		return mShowsTouchWhenHighlighted;
	}
	
	public void setAdjustsImageWhenHighlighted(boolean adjusts) {
		mAdjustsImageWhenHighlighted = adjusts;
		if (adjusts) {
			if (mShadowView == null) { 
				mShadowView = new UIView(bounds());
				mShadowView.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
				mShadowView.setBackgroundColor(new UIColor(0, 0, 0, 0.5f)); 
				if (mBackgroundView != null) {
					insertSubviewAboveSubview(mShadowView, mBackgroundView); 
				} else {
					insertSubviewAtIndex(mShadowView, 0); 
				}
			}
			if(((state() & UIControlStateHighlighted) == UIControlStateHighlighted) &&
					backgroundImageForState(UIControlStateHighlighted) == null) { 
				mShadowView.setHidden(false);
			} else {
				mShadowView.setHidden(true);
			}
		} else {
			if (mShadowView != null) {
				mShadowView.removeFromSuperview();
				mShadowView = null; 
			}
		}
	}
	
	public boolean isAdjustsImageWhenHighlighted() {
		return mAdjustsImageWhenHighlighted;
	}
	
	public void setAdjustsImageWhenDisabled(boolean adjusts) {
		mAdjustsImageWhenDisabled = adjusts;
		if (adjusts) {
			if(((state() & UIControlStateDisabled) == UIControlStateDisabled)) { 
				setAlpha(0.6f);
			} else {
				setAlpha(1);
			}
		} else {
			setAlpha(1);
		}
	}
	
	public boolean isAdjustsImageWhenDisabled() {
		return mAdjustsImageWhenDisabled;
	}
	
	public void setContentStretch(CGRect stretch) {
		super.setContentStretch(stretch);
		if (mBackgroundView != null) {
			mBackgroundView.setContentStretch(stretch); 
		}
	}
	
	public void setFont(UIFont font) {
		mFont = font;
		if (mTitleLabel != null) {
			mTitleLabel.setFont(font);
		}
	}
	
	public UIFont font() {
		return mFont;
	}
	
	public void setTitleEdgeInsets(UIEdgeInsets insets) {
		mTitleEdgeInsets = new UIEdgeInsets(insets); 
		if (mTitleLabel != null) {
			CGRect bounds = bounds().inset(mTitleEdgeInsets); 
			mTitleLabel.setFrame(bounds); 
		}
	}
	
	public UIEdgeInsets titleEdgeInsets() {
		return new UIEdgeInsets(mTitleEdgeInsets);
	}
	
	public void setImageEdgeInsets(UIEdgeInsets insets) {
		mImageEdgeInsets = new UIEdgeInsets(insets); 
		if (mImageView != null) {
			CGRect bounds = bounds().inset(mImageEdgeInsets); 
			mImageView.setFrame(bounds); 
		}
	}
	
	public UIEdgeInsets imageEdgeInsets() {
		return new UIEdgeInsets(mImageEdgeInsets);
	}
	
	protected void onStateChanged(int oldState, int newState) {
		boolean done = false;
		
		if(mTitleLabel != null) {
			
			for (NSObject o : mColors.allKeys()) {
				int state = ((NSNumber)o).intValue();
				if (state != UIControlStateNormal && (state & newState) == state) {
					UIColor color = (UIColor) mColors.objectForKey(o);
					mTitleLabel.setTextColor(color);
					done = true;
					break;
				}
			}
			
			if (!done) { 
				UIColor color = (UIColor) mColors.objectForKey(new NSNumber(UIControlStateNormal)); 
				if (color == null) {
					color = UIColor.blackColor();
				}
				mTitleLabel.setTextColor(color);
			}
			
			done = false;
			
			for (NSObject o : mTitles.allKeys()) {
				int state = ((NSNumber)o).intValue();
				if (state != UIControlStateNormal && (state & newState) == state) {
					NSString title = (NSString) mTitles.objectForKey(o);
					mTitleLabel.setText(title);
					done = true;
					break;
				}
			}
			
			if (!done) { 
				NSString title = (NSString) mTitles.objectForKey(new NSNumber(UIControlStateNormal)); 
				mTitleLabel.setText(title);
			}
			
			done = false;
			
			
		}
		
		if (mImageView != null) {
			
			for (NSObject o : mImages.allKeys()) {
				int state = ((NSNumber)o).intValue();
				if (state != UIControlStateNormal && (state & newState) == state) {
					UIImage image = (UIImage) mImages.objectForKey(o);
					mImageView.setImage(image);
					done = true;
					break;
				}
			}
			
			if (!done) { 
				UIImage image = (UIImage) mImages.objectForKey(new NSNumber(UIControlStateNormal)); 
				mImageView.setImage(image);
			}
			
			done = false;
			
		}
		
		if (mBackgroundView != null) {
			
			for (NSObject o : mBackgroundImages.allKeys()) {
				int state = ((NSNumber)o).intValue();
				if (state != UIControlStateNormal && (state & newState) == state) {
					UIImage image = (UIImage) mBackgroundImages.objectForKey(o);
					mBackgroundView.setImage(image);
					done = true;
					break;
				}
			}
			
			if (!done) { 
				UIImage image = (UIImage) mBackgroundImages.objectForKey(new NSNumber(UIControlStateNormal)); 
				mBackgroundView.setImage(image);
			} 
			
		}
		
		if (mGlowView != null) { 
			if((newState & UIControlStateHighlighted) == UIControlStateHighlighted) {
				mGlowView.setHidden(false);
			} else {
				mGlowView.setHidden(true);
			}
		}
		
		if (mShadowView != null) { 
			if(((newState & UIControlStateHighlighted) == UIControlStateHighlighted) &&
					backgroundImageForState(UIControlStateHighlighted) == null) { 
				mShadowView.setHidden(false);
			} else {
				mShadowView.setHidden(true);
			}
		}
		
		if (mAdjustsImageWhenDisabled) {
			if(((newState & UIControlStateDisabled) == UIControlStateDisabled)) { 
				setAlpha(0.6f);
			} else {
				setAlpha(1);
			}
		}
		
	}
	
}
